package com.rms.controller;


import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rms.common.Const;
import com.rms.entity.DinnerTable;
import com.rms.entity.Food;
import com.rms.entity.FoodType;

import com.rms.utils.Condition;
import com.rms.utils.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping(value = "/index")
public class IndexAction extends BaseAction  {

	@RequestMapping(value = "/welcome")
	public String welcome(HttpServletRequest request){
		List<DinnerTable> dinnerTables =dinnerTableService.query();

		request.getServletContext().setAttribute(Const.DINNERTABLES_ATTR_NAME,dinnerTables);

		return "/app/index.jsp";
	}

	@RequestMapping(value = "/getMenu")
	public String getMenu(String table_id,HttpServletRequest request){

		HttpSession session = request.getSession();// 用于存储订单信息
		// 获取session里的值
		Object obj = session.getAttribute("table_id");


		if (table_id != null) {
			dinnerTableService.changeState(Integer.parseInt(table_id));
			if (obj == null) {
				session.setAttribute("table_id", table_id);// 存放桌id以备订单用
			}
		}

		// 查询菜系信息
		List<FoodType> foodtypes = foodTypeService.query();
		request.setAttribute("foodtypes", foodtypes);

		// 获取菜单页面信息
		PageBean<Food> pb = new PageBean<Food>();

		Condition con = new Condition();
		// 获取页面得到的参数
		String foodtype = request.getParameter("foodtype");
		String foodName = request.getParameter("foodName");
		if (foodtype != null && !foodtype.isEmpty()) {
			con.setFoodType_id(Integer.parseInt(foodtype));
			pb.setCondition(con);
		}
		if (foodName != null && !foodName.isEmpty()) {
			con.setFoodName(foodName);
			pb.setCondition(con);
		}

		pb.setPageCount(6);
		String curPage = request.getParameter("currentPage");// 获取当前页
		if (curPage == null || curPage.isEmpty()) {
			pb.setCurrentPage(1);
		}
		if (curPage != null && !curPage.isEmpty()) {
			int currentPage = Integer.parseInt(curPage);
			pb.setCurrentPage(currentPage);
		}

		foodService.getAll(pb);

		request.setAttribute("pageBean", pb);
		// 跳转
		return "/app/detail/caidan.jsp";
	}

	@RequestMapping(value = "/searchFood")
	public String searchFood(HttpServletRequest request,
			HttpServletResponse response){

		
		PageBean<Food> pb = new PageBean<Food>();
		Condition condition = new Condition();
		
		String keyword = request.getParameter("keyword");//设置关键词
		if(keyword!=null && !keyword.isEmpty()){
			condition.setFoodName(keyword);
		}
		if(condition!=null){
			pb.setCondition(condition);
		}
		pb.setPageCount(6);
		pb.setCondition(condition);
		
		foodService.getAll(pb);

		request.setAttribute("pageBean", pb);
		// 跳转
		return "/app/detail/caidan.jsp";
	}


	@RequestMapping(value = "/getFoodDetail")
	public String getFoodDetail(HttpServletRequest request, HttpServletResponse response)
	{

		String id = request.getParameter("food");//获取食物id
		Food food = foodService.findById(Integer.parseInt(id));
		List<FoodType> foodtypes = foodTypeService.query();
		request.setAttribute("food", food);
		request.setAttribute("foodtypes", foodtypes);
		
		return "/app/detail/caixiangxi.jsp";
	}

}
